<?php namespace App\Http\Controllers;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;
use App\Order;
use App\Http\Resources\OrderResource;
use App\Item;
use Illuminate\Support\Facades\DB;
use App\User;

class OrderController extends Controller {

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
    public  function index(Request $request){//用于搜索查询
          
        $query=Order::orderBy('createTime','desc');
        $paginate=10;
        if($request->has('paginate')){
            $paginate=$request->get('paginate');
        }     
        if($request->has('orderid')){
            $orderId=$request->get('orderid');
            $query->where('orderid','like','%'.$orderId.'%');
        }
        if($request->has('offlineid')){
            $offlineId=$request->get('offlineid');
            $query->where('offlineId', $offlineId);
        }
        if($request->has('status')){
               $status=$request->get('status');
               if($status=='60' || $status=='40'){
                   $query->where('status',$status);
               }else if($status=="close"){
                    $query->where('closeType','<>','0');
               }else if($status!=="all"){//既不等于all也不等于60或者40 
                   $query->where('status',$status)->where('closeType','0');
               }
        }
        if($request->has('pickerid')){
            $pickerId=$request->get('pickerid');
            $query->where('pickerId',$pickerId);
        }
        if($request->has('riderid')){
            $riderId=$request->get('riderid');
            $query->where('riderId',$riderId);
        }
        if($request->has('date')){
              $date=$request->get('date');
              $createTime1=$date[0];
              $createTime1=date("Y-m-d:00-00-00", strtotime($createTime1));
              $createTime2=$date[1];
              $createTime2=date("Y-m-d 23:59:59", strtotime($createTime2));
              $query->where('createTime','>=',$createTime1)->where('createTime','<=',$createTime2);
        }else{
            $createTime1=date("Y-m-d:00-00-00", strtotime("-1 week"));
            $createTime2=date("Y-m-d 23:59:59");
            $query->where('createTime','>=',$createTime1)->where('createTime','<=',$createTime2);
        }
        
       if($request->has('excel')){
             $excel=$request->get('excel');
             $data=$query->with('rider')->with('picker')->with('items')->take(500)->get();
       }else{
             $data=$query->with('rider')->with('picker')->with('items')->paginate($paginate);
       }
          
        $res=$data;
        foreach ($data as $key=> $value){
            if($value->dispatchTime&&$value->createTime){
            $time=strtotime($value->createTime)-strtotime($value->dispatchTime);
            $res[$key]->waittime=date('h:i:s',$time);
            }else{
             $res[$key]->waittime='';
        }
            if($value->finishTime&&$value->pickTime){
            $time=strtotime($value->finishTime)-strtotime($value->pickTime);
            $res[$key]->jiantime=date('h:i:s',$time);
            }else{
             $res[$key]->jiantime='';
        }
        if($value->endTime&&$value->riderTime){
            $time=strtotime($value->endTime)-strtotime($value->riderTime);
            $res[$key]->peisongtime=date('h:i:s',$time);
            }else{
             $res[$key]->peisongtime='';
           }
        }
         $data=$res;
         return $data;
//         return response()->json(['data'=>$data]);
    }
    public  function chart(Request $request){
        $query=Order::orderBy('createTime','desc');
        if($request->has('date')){
            $createTime1=$request->get('date');
        }else{
            $createTime1=date('Y-m-01', strtotime(date("Y-m-d")));
        }
            $createTime1=date("Y-m-d 00:00:00",strtotime($createTime1));
            $createTime2=date("Y-m-d 00:00:00",strtotime("+1 months", strtotime($createTime1)));
            $query->where('createTime','>=',$createTime1)->where('createTime','<',$createTime2);
        if($request->has('userid')){
            $userId=$request->get('userid');
            $user=User::find($userId);
            if(!$user){
               $type=3;
            }else{
              $type=$user->type;
            }
            if($type==1){
            $query->where('pickerId',$userId);
            }elseif($type==2){
            $query->where('riderId', $userId);
            }
            else{
                $query->where('riderId', $userId);
            }
        }
        if($request->has('reason')){
            $reason=$request->get('reason');
            if($reason=='normal'){
               $query->whereNotNull('finishReason')->where('status',60)->where('closeType','0');    
            }elseif($reason=="disabled"){
               $query->whereNull('finishReason')->where('status',60)->where('closeType','0');  
            }elseif($reason=="closed"){
                $query->where('closeType','!=','0'); 
            }else{
            }
        }
        $data=$query->get();
        $res=[];
        if(!$data){
               return $res;
           }
//        $start=$createTime1;
//        $end=date("Y-m-d",strtotime("+1 day", strtotime($createTime1)));
//        for($i=0;$i>31;$i++){
//        $query->where('createTime','>=',$start)->where('createTime','<',$createTime2);
//        }
        for($i=0;$i<31;$i++){
              $count=0;
          foreach ($data as $value) {
          $date=date("d",strtotime($value['createTime']));
          $date=$date-1;
            if($i==$date){
                $count++;
            }
        }   
            $res[]=$count;
        }  
            return  $res;
//          return response()->json(['data'=>$data]);
    }
    
    
    
    public  function get_or_data(Request $request){//同时获取订单列表、拣货员列表与配送员列表  只用于第一次渲染订单列表
        
        $query=Order::orderBy('createTime','desc');
        $paginate=10;
        $createTime1=date("Y-m-d:00-00-00", strtotime("-1 week"));
        $createTime2=date("Y-m-d 23:59:59");
        $query->where('createTime','>=',$createTime1)->where('createTime','<=',$createTime2);
        if($request->has('offlineid')){
            $offlineId=$request->get('offlineid');
            $query->where('offlineId', $offlineId);
        }else{
            $offlineId='';
        }
        $query1=$query;
        $orderdata=$query->with('rider')->with('picker')->with('items')->paginate($paginate);//分页数据
        $total=$orderdata->total();
        $last_page=$orderdata->lastPage();
        $current_page=$orderdata->currentPage();
        
        
        $orderlist=$query->with('rider')->with('picker')->with('items')->take($paginate)->get();//取10条
        $res=$orderlist;
        foreach ($orderlist as $key=> $value){
            if($value->dispatchTime&&$value->createTime){
            $time=strtotime($value->createTime)-strtotime($value->dispatchTime);
            $res[$key]->waittime=date('h:i:s',$time);
            }else{
             $res[$key]->waittime='';
        }
            if($value->finishTime&&$value->pickTime){
            $time=strtotime($value->finishTime)-strtotime($value->pickTime);
            $res[$key]->jiantime=date('h:i:s',$time);
            }else{
             $res[$key]->jiantime='';
        }
        if($value->endTime&&$value->riderTime){
            $time=strtotime($value->endTime)-strtotime($value->riderTime);
            $res[$key]->peisongtime=date('h:i:s',$time);
            }else{
             $res[$key]->peisongtime='';
           }
        }
         $orderlist=$res;
         $pickerlist=User::orderBy('createTime','desc')->where('offlineId',$offlineId)->where('type','1')->get();
         $riderlist=User::orderBy('createTime','desc')->where('offlineId',$offlineId)->where('type','2')->get();

         
         $data['pickerlist']=$pickerlist;
         $data['riderlist']=$riderlist;
         $data['orderlist']= $orderlist;
         $data['total']=$total;
         $data['last_page']=$last_page;
         $data['current_page']=$current_page;
         return $data;
    }
    public function info(Request $request){
        $orderid=$request->get('orderId');
        $order=Order::where('orderId',$orderid)->first();
        $item=Item::where('orderId',$orderid)->first();
        $buyerMessage=$order->buyerMessage;
        $outitems=$item->outItems;
        $outitems= explode(',', $outitems);
        $str=$item->orders;
        $data=json_decode($str,true);
        if(!$data){
            return $data;
        }
         $res=$data;
        foreach ($data as $k=>$v){
            if(in_array($v['item_id'],$outitems)){
                 $res[$k]['is_short']='缺货';
            }else{
                 $res[$k]['is_short']='--';
            }
        }
        $result['data']=$res;
        $result['buyerMessage']=$buyerMessage;
           $data=$result;
         return $data;
    }
    public function handleorder(Request $request){//通过得到的订单Id判断状态  通过状态判断要修改的是拣货员还是配送员 获取除了这个用户之外的类型用户
        
         $orderid=$request->get('orderId');
         $order=Order::where('orderId',$orderid)->first();
         $offlineid=$order->offlineId;
//         dd($order->rider->nick);
         $status=$order->status;
         if($status==20 or $status==30){
           $usertype=1;
           $userid=$order->pickerId;
         }
         if($status==50){
            $usertype=2;
            $userid=$order->riderId;
         }
         $data=User::where('offlineId',$offlineid)->where('type',$usertype)->where('id','<>',$userid)->get();
         return response()->json(['data'=>$data]);
    }
         
         
         
    public  function  getitem(){
            $item=Item::find(1);
            $order=$item->order;
            dd($order);
    }
         
    public function getorder(){
            $order=Order::with('items');
            dd($order);
         }
    public function getpicker(){
            $order=Order::with('rider')->with('picker')->get();
            dd($order);
         }
         
    public  function getexcel(){
        
               $status=$request->get('status');
               if($status=='60' || $status=='40'){
                   $query->where('status',$status);
               }else if($status=="close"){
                    $query->where('closeType','<>','0');
               }else if($status!=="all"){//既不等于all也不等于60或者40 
                   $query->where('status',$status)->where('closeType','0');
               }
        if($request->has('pickerid')){
            $pickerId=$request->get('pickerid');
            $query->where('pickerId',$pickerId);
        }
        if($request->has('riderid')){
            $riderId=$request->get('riderid');
            $query->where('riderId',$riderId);
        }
        if($request->has('date')){
              $date=$request->get('date');
              $createTime1=$date[0];
              $createTime1=date("Y-m-d:00-00-00", strtotime($createTime1));
              $createTime2=$date[1];
              $createTime2=date("Y-m-d 23:59:59", strtotime($createTime2));
              $query->where('createTime','>=',$createTime1)->where('createTime','<=',$createTime2);
        }else{
            $createTime1=date("Y-m-d:00-00-00", strtotime("-1 week"));
            $createTime2=date("Y-m-d 23:59:59");
            $query->where('createTime','>=',$createTime1)->where('createTime','<=',$createTime2);
        }
         $data=$query->with('rider')->with('picker')->with('items')->take(500)->get();
         $res=$data;
        foreach ($data as $key=> $value){
            if($value->dispatchTime&&$value->createTime){
            $time=strtotime($value->createTime)-strtotime($value->dispatchTime);
            $res[$key]->waittime=date('h:i:s',$time);
            }else{
             $res[$key]->waittime='';
        }
            if($value->finishTime&&$value->pickTime){
            $time=strtotime($value->finishTime)-strtotime($value->pickTime);
            $res[$key]->jiantime=date('h:i:s',$time);
            }else{
             $res[$key]->jiantime='';
        }
        if($value->endTime&&$value->riderTime){
            $time=strtotime($value->endTime)-strtotime($value->riderTime);
            $res[$key]->peisongtime=date('h:i:s',$time);
            }else{
             $res[$key]->peisongtime='';
           }
        }
         $data=$res;
         return $data;
    }    
         
         
}
